package com.yiding.sys.common;

import com.yiding.sys.common.model.Result;
import com.yiding.sys.common.enums.ResultCodeEnum;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author War horse imwarhorse@aliyun.com
 * Date: 2020/8/7
 * Description: 统一异常处理类
 */

@RestControllerAdvice
public class DefaultExceptionHandler {

	@ExceptionHandler(AuthenticationException.class)
	public Result authenticationExceptionHandler(AuthenticationException ae) {
		System.err.println("============================================================");

		if (ae instanceof IncorrectCredentialsException) {
			return new Result().geneFailed(ResultCodeEnum.CREDENTIALS_ILLEGAL);
		} else if (ae instanceof LockedAccountException) {
			return new Result().geneFailed(ResultCodeEnum.LOCKED_ACCOUNT);
		} else {
			return new Result().geneFailed(ResultCodeEnum.FAILED);
		}
	}

}
